/**
 * 
 */
package homework6;

import java.util.LinkedList;

/**
 * @author Nican
 * 
 */
public class CellRef implements IFormula {
	String cellname;
	
	/**
	 * Starts a new cellref, with the given cellname
	 * @param cellname
	 */
	CellRef(String cellname) {
		this.cellname = cellname;
	}

	/**
	 * Traverse the value, visitor method
	 */
	public <R> R traverse(IProc<R> f) {
		return f.processCellRef(this);
	}

	/**
	 * Checks if two cellrefs are of the same reference
	 */
	public boolean equals(Object o) {
		return (o instanceof CellRef && ((CellRef) o).cellname
				.equals(this.cellname));
	}

	/**
	 * Get the value of the cellref by looking it up in the spreadsheet
	 * @param spreadSheet
	 * @param usedCellRefs
	 * @return
	 */
	public Integer getValue(SpreadSheet spreadSheet,
			LinkedList<CellRef> usedCellRefs) {
		return spreadSheet.lookupValue(this.cellname, usedCellRefs);
	}

}
